﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Senparc.Weixin;
using Senparc.Weixin.MP.AdvancedAPIs.User;
using Senparc.Weixin.MP.Containers;

namespace WXApi.Controllers
{
    public class OpenController : Controller
    {

        public static readonly string EncodingAESKey = Config.SenparcWeixinSetting.EncodingAESKey;//与微信公众账号后台的EncodingAESKey设置保持一致，区分大小写。
        public static readonly string AppId = Config.SenparcWeixinSetting.WeixinAppId;//与微信公众账号后台的AppId设置保持一致，区分大小写。
        public static readonly string appSecret = Config.SenparcWeixinSetting.WeixinAppSecret;//与微信公众账号后台的AppSecret设置保持一致，区分大小写。
        public static string access_token = "";
        [HttpGet]
        [ActionName("Index")]
        public ActionResult Get(int cmd)
        {
            string sre = "";
            switch (cmd)
            {
                case 1:
                    sre = SreOpenId();
                    break;
                case 2:

                    break;
                default:
                    break;
            }
            return Content(sre); //返回随机字符串则表示验证通过 
        }


        public string SreOpenId()
        {
            if (access_token=="")
            {
                if (!AccessTokenContainer.CheckRegistered(AppId))//检查是否已经注册
                {
                    AccessTokenContainer.Register(AppId, appSecret);//如果没有注册则进行注册
                }
               access_token = AccessTokenContainer.GetAccessTokenResult(AppId).access_token;
            }
           
            return JsonConvert.SerializeObject(Common.Get(access_token,""));
        }
        
    }
}